package com.leiou.stu_reg;

import com.leiou.pojo.Student;
import com.leiou.utils.ConnectionDatabaseForSQLSeverUseJdbc;

import java.sql.*;

public class stuRegDaoImpl implements stuRegDao {
    Connection connection=null;
    PreparedStatement pstmt=null;
    ResultSet rset=null;

    public stuRegDaoImpl(){
        connection= ConnectionDatabaseForSQLSeverUseJdbc.getConnection();
    }

    //学生注册
    @Override
    public int insertStudent(Student student) {
        PreparedStatement pstmt = null;
        try {
            String sql = "INSERT INTO student (name, gender, birthDate, idCard, phone, "
                    + "email, emergencyContact, username, password) "
                    + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
            pstmt = connection.prepareStatement(sql);

            int index = 1;
            pstmt.setString(index++, student.getName());
            pstmt.setString(index++, student.getGender());
            pstmt.setDate(index++, java.sql.Date.valueOf(student.getBirthDate()));
            pstmt.setString(index++, student.getIdCard());
            pstmt.setString(index++, student.getPhone());
            pstmt.setString(index++, student.getEmail());
            pstmt.setString(index++, student.getEmergencyContact());
            pstmt.setString(index++, student.getUsername());
            pstmt.setString(index++, student.getPassword());

            int affectedRows = pstmt.executeUpdate();
            if (affectedRows == 0) {
                throw new SQLException("创建用户失败，无数据插入");
            }
            return affectedRows;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

//    检查登录名是否重复
    @Override
    public boolean checkusername(String username) {
        try {
            pstmt = connection.prepareStatement("SELECT TOP 1 username FROM student WHERE username = ?");
            pstmt.setString(1,username);

            rset=pstmt.executeQuery();

            if (rset.next())
                return false;
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Student selectStu(String username) {
        try {
            String sql = "SELECT studentId, name, gender, birthDate, idCard, college, major, "
                    + "className, status, dormId, bedNumber, phone, email, emergencyContact, "
                    + "username, password FROM student WHERE username = ?";
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, username);
            rset = pstmt.executeQuery();
            if (rset.next()) {
                Student student = new Student();
                student.setStudentId(rset.getString("studentId"));
                student.setName(rset.getString("name"));
                student.setGender(rset.getString("gender"));
                Date birthDate = rset.getDate("birthDate");
                if (birthDate != null) {
                    student.setBirthDate(birthDate.toLocalDate());
                }
                student.setIdCard(rset.getString("idCard"));
                student.setCollege(rset.getString("college"));
                student.setMajor(rset.getString("major"));
                student.setClassName(rset.getString("className"));
                student.setStatus(rset.getInt("status"));
                student.setDormId(rset.getInt("dormId"));
                student.setBedNumber(rset.getString("bedNumber"));
                student.setPhone(rset.getString("phone"));
                student.setEmail(rset.getString("email"));
                student.setEmergencyContact(rset.getString("emergencyContact"));
                student.setUsername(rset.getString("username"));
                student.setPassword(rset.getString("password"));
                return student;
            }
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }


}
